package control.micro
{
	import flash.events.EventDispatcher;
	import model.micro.Battle;
	import model.micro.FieldUnit;
	import model.micro.RangedFieldUnit;
	
	/**
	 */
	public class BattleController extends EventDispatcher
	{
		protected var unit_controllers:Vector.<FieldUnitController>;
		protected var battle:Battle;
		
		public function BattleController(battle:Battle)
		{
			this.battle = battle;
			unit_controllers = new Vector.<FieldUnitController>();
		}
		
		public function update():void
		{
			for (var i:int = unit_controllers.length - 1; i >= 0; i--)
			{
				if (unit_controllers[i].readyToRemove())
				{
					unit_controllers.splice(i, 1);
				}
				else
				{
					unit_controllers[i].update();
				}
			}
		}
		
		/**
		 * Create a controller for the unit.
		 * @param	unit
		 */
		protected function addUnitController(unit:FieldUnit):void
		{
			var controller:FieldUnitController;
			if (unit is RangedFieldUnit)
			{
				controller = new RangedFieldUnitController((unit as RangedFieldUnit), battle);
			}
			else
			{
				controller = new FieldUnitController(unit, battle);
			}
			unit.controller = controller;
			unit_controllers.push(controller);
		}
	}
}